package com.sunrun.sunrunframwork.uiutils.paging;


import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.sunrun.sunrunframwork.utils.EmptyDeal.empty;
import static com.sunrun.sunrunframwork.utils.EmptyDeal.size;

/**
 * 分页代理类
 * Created by WQ on 2017/5/26.
 */

public class PageLimitDelegate<T extends UniqueEntity> {
    protected int START_PAGE = 1;
    protected int page = START_PAGE;//当前页
    protected DataProvider provider;//数据提供者,主要实现loadData 方法即可
    protected IAdapter<T> iAdapter;//适配器
    protected IRefresh iRefresh;//下拉刷新控件
    protected AtomicBoolean isLoadMore = new AtomicBoolean(false);//是否处于加载状态
    protected int maxPageSize = 10;//每页最大值

    public PageLimitDelegate(DataProvider provider) {
        this.provider
                = provider;
    }

    protected boolean isAttach = false;

    /**
     * 附加列表信息,设置加载刷新信息
     */
    public void attach(IRefresh iRefresh, IAdapter<T> iAdapter) {
        this.iAdapter = iAdapter;
        this.iRefresh = iRefresh;
        iRefresh.setEnabled(true);
        iRefresh.setEnableLoadMore(true);
        loadCurrentPage();
        isAttach = true;
    }

    public void loadCurrentPage() {
        provider.loadData(page);
    }

    public void loadPage(int page) {
        provider.loadData(page);
    }

    public void loadMorePage() {
        isLoadMore.set(true);
        page++;
        provider.loadData(page);
    }

    public int getStartPage() {
        return START_PAGE;
    }

    public void setStartPage(int START_PAGE) {
        this.START_PAGE = START_PAGE;
    }

    public int getMaxPageSize() {
        return maxPageSize;
    }

    public void setMaxPageSize(int maxPageSize) {
        this.maxPageSize = maxPageSize;
    }

    /**
     * 刷新
     */
    public void refreshPage() {
        page = 1;
        isLoadMore.set(false);
        provider.loadData(page);
    }

    /**
     * 设置数据,
     *
     * @param data
     */
    public void setData(List<T> data) {
        maxPageSize = Math.max(maxPageSize, size(data));
        if (isLoadMore.get() || page != START_PAGE) {
            if (empty(data)) {
                page--;
                iRefresh.setEnableLoadMore(false);
                iRefresh.loadMoreComplete();
            } else {
                iAdapter.addData(data);
            }
            loadComplete();
        } else {
            loadComplete();
            iAdapter.setNewData(data);
            iRefresh.setEnableLoadMore(size(data) >= maxPageSize);
        }
    }

    /**
     * 加载/刷新完成
     */
    public void loadComplete() {
        isLoadMore.set(false);
        iRefresh.setRefreshing(false);
        iRefresh.loadMoreComplete();

    }

    /**
     * 数据提供者接口,用于进行加载动作
     */
    public interface DataProvider {
        public void loadData(int page);
    }


    public static void addData2List(List src, List data) {
        if (data != null) {
            Iterator var3 = data.iterator();

            while (true) {
                while (var3.hasNext()) {
                    Object object = var3.next();
                    int index = src.indexOf(object);
                    if (index != -1) {
                        src.set(index, object);
                    } else {
                        src.add(object);
                    }
                }

                return;
            }
        }
    }

    public boolean isAttach() {
        return isAttach;
    }
}
